package com.mall.admin.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.mall.commons.utils.PageResult;
import com.mall.domain.SmsCoupon;
import com.mall.domain.SmsCouponProductCategoryRelation;
import com.mall.domain.SmsCouponProductRelation;
import com.mall.dto.CouponDto;
import com.mall.interface1.api.SmsCouponApi;
import com.mall.vo.CouponVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SmsCouponService {

    @DubboReference
    private SmsCouponApi smsCouponApi;

    //添加优惠券
    public void addCoupon(CouponDto dto) {
        //将优惠券信息存入优惠券表
        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtil.copyProperties(dto,smsCoupon);
        //存入优惠券表并返回优惠券id
        Long couponId = smsCouponApi.addCoupon(smsCoupon);

        List<SmsCouponProductCategoryRelation> scpcrList = dto.getProductCategoryRelationList();
        List<SmsCouponProductRelation> scprList = dto.getProductRelationList();

        if(CollUtil.isNotEmpty(scpcrList)){
            for (SmsCouponProductCategoryRelation item : scpcrList) {
                item.setCouponId(couponId);
                smsCouponApi.addScpcr(item);
            }
        }
        if(CollUtil.isNotEmpty(scpcrList)){
            for (SmsCouponProductRelation item : scprList) {
                item.setCouponId(couponId);
                smsCouponApi.addScpr(item);
            }
        }
    }

    //删除优惠券
    public void deleteCoupon(Long id) {
        if (id != null){
            smsCouponApi.deleteCouponById(id);
            //删除相关关联数据
            smsCouponApi.deletescpcr(id);
            smsCouponApi.deletescpr(id);
        }
    }

    //根据优惠券名称和类型分页获取优惠券列表
    public PageResult list(String name, Integer pageNum, Integer pageSize, Integer type) {
        //根据条件分页查询出优惠券列表
        List<SmsCoupon> scList = smsCouponApi.list(name,pageNum,pageSize,type);
        //查询符合条件总记录条数
        Integer count = smsCouponApi.count(name,pageNum,pageSize,type);
        PageResult pageResult = new PageResult(pageNum,pageSize,count,scList);
        return pageResult;
    }

    //修改优惠券
    public void update(Long id, CouponDto dto) {
        //修改优惠券表
        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtil.copyProperties(dto,smsCoupon);
        smsCouponApi.update(id,smsCoupon);
        //修改相关表
        List<SmsCouponProductCategoryRelation> scpcrList = dto.getProductCategoryRelationList();
        List<SmsCouponProductRelation> scprList = dto.getProductRelationList();

        if(CollUtil.isNotEmpty(scpcrList)){
            for (SmsCouponProductCategoryRelation item : scpcrList) {
                smsCouponApi.updateScpcr(id,item);
            }
        }
        if(CollUtil.isNotEmpty(scpcrList)){
            for (SmsCouponProductRelation item : scprList) {
                smsCouponApi.updateScpr(id,item);
            }
        }
    }

    //获取单个优惠券的详细信息
    public CouponVo findCoupon(Long id) {
        CouponVo vo = new CouponVo();
        //获取单个优惠券的详细信息
        SmsCoupon smsCoupon = smsCouponApi.findById(id);
        BeanUtil.copyProperties(smsCoupon,vo);
        //获取单个优惠券的相关信息
        List<SmsCouponProductCategoryRelation> scpcrList = smsCouponApi.findScpcr(id);
        List<SmsCouponProductRelation> scprList = smsCouponApi.findScpr(id);
        vo.setProductCategoryRelationList(scpcrList);
        vo.setProductRelationList(scprList);
        return vo;
    }
}
